// miniprogram/pages/profile-playlike/profile-playlike.js
const app = getApp()
let saveMusiclist
Page({

  /**
   * 页面的初始数据
   */
  data: {
    musiclist: []
  },
  search(event) {
    wx.setStorageSync('musiclist', saveMusiclist)
   let keyword = event.detail
   let searchList = [] 
   let musiclist = wx.getStorageSync('musiclist')
   musiclist.map((item) => {
    if(item.name.includes(keyword) || item.ar[0].name.includes(keyword)) {
      searchList.push(item)
    }
   })
   this.setData({
     musiclist: searchList
   })
   wx.setStorageSync('musiclist', searchList)
  },
  onChange(event) {
    if(!event.detail){
      wx.setStorageSync('musiclist', saveMusiclist)
      this.setData({
        musiclist: saveMusiclist
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const openid = app.globalData.openid
    wx.cloud.database().collection('user-like').where({ _openid: openid
    }).get().then((res) =>{
      let likeList = []
      if(res.data[0]) likeList = res.data[0].like_sings 
      if(likeList.length) {
        this.setData({
          musiclist: likeList
        })
        saveMusiclist = likeList
        wx.setStorageSync('musiclist',saveMusiclist)
      } else {
        wx.showModal({
          title: '当前收藏内容为空',
          content: ''
        })
      }
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})